package com.lovo.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lovo.dao.IUserDAO;
import com.lovo.util.bean;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.annotation.MapperScans;
import org.springframework.context.annotation.*;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.io.InputStream;

/**
 * Description:
 *
 * @author LiuChengQi
 * @date Created on 2020/11/14
 */
//扫描basePackages下所有接口, 等同于mybaties映射包,
// 同时把实现类注册进spring容器, 管理事务
@MapperScan(basePackages = "com.lovo.dao")
//让这个类变成配置类, 来完全消除xml
@Configuration
//扫描basePackages下所有带Component注解的东西, 注册进spring容器
@ComponentScan(basePackages = "com.lovo")
//加载指定配置文件
@PropertySource("classpath:db.properties")
//代理类有接口要写这个proxyTargetClass = true
@EnableAspectJAutoProxy(proxyTargetClass = true)
//打开事务
@EnableTransactionManagement

public class AppConfig {
    @Resource
    private DBProperties dbProperties;
    //创建事务管理器对象
    @Bean
    public PlatformTransactionManager transactionManager(DataSource dataSource) {
        DataSourceTransactionManager dataSourceTransactionManager =
                new DataSourceTransactionManager(dataSource);
        return dataSourceTransactionManager;
    }

    @Bean
    //数据源
    public DataSource dataSource(){
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(dbProperties.getDbUrl());
        dataSource.setDriverClassName(dbProperties.getDbDriver());
        dataSource.setUsername(dbProperties.getDbUser());
        dataSource.setPassword(dbProperties.getDbPwd());
        return dataSource;
    }

    //把下面这个对象放到容器里, 名字是方法名
    @Bean
    public ObjectMapper objectMapper(){
        return new ObjectMapper();
    }



    @Bean
    public SqlSessionFactoryBean sqlSessionFactory() throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        //设置数据源
        factoryBean.setDataSource(dataSource());
        return factoryBean;

    }

//    @Bean
//    public IUserDAO userDAO() throws Exception {
//        return sqlSession().getMapper(IUserDAO.class);
//    }
//    @Bean
//    public SqlSessionTemplate sqlSession() throws Exception {
//        return new SqlSessionTemplate(sqlSessionFactory());
//    }
}
